package com.glorysoft.sunnypda.ui.eqpmovein

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivityEqpMoveInBinding
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.ui.autograph.AutographActivity
import com.glorysoft.sunnypda.ui.eqpinfo.EqpInfoActivity
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_eqp_move_in.*
import kotlinx.android.synthetic.main.activity_eqp_move_in.etEqpWork
import kotlinx.android.synthetic.main.activity_eqp_move_in.flFabList
import kotlinx.android.synthetic.main.activity_eqp_move_in.tvAddEqp

/**
 * FileName: EqpMoveInActivity
 * Author: XJH
 * Date: 2022/1/11 15:35
 * Description:
 * History:
 */
class EqpMoveInActivity : BaseActivity<EqpMoveInViewModel, ActivityEqpMoveInBinding>(),
    View.OnClickListener {

    private var fabPop: PopWindowsUtils.GeneralPop? = null
    private var linePop: PopWindowsUtils.GeneralPop? = null
    private var posPop: PopWindowsUtils.GeneralPop? = null


    override fun layoutId(): Int {
        return R.layout.activity_eqp_move_in
    }

    override fun initVariableId(): Int {
        return BR.eqpMoveInViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.equipment_moved_in))
        viewModel.getFabList()
//        viewModel.initList()

        tvAddEqp.setOnClickListener(this)
        flFabList.setOnClickListener(this)
        flLineList.setOnClickListener(this)
        flPosList.setOnClickListener(this)
        setRightListener(getString(R.string.confirm)) {
            if (viewModel.eqpItems.size <= 0){
                viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.add_main_eqp),false)
                return@setRightListener
            }
            val intent = Intent(this, AutographActivity::class.java)
            startActivityForResult(intent, 1000)
        }

        initEdit(etEqpWork, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.getEqpInfoMovein(s)
            }

        })
    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this) {
            val intent = Intent(this, EqpInfoActivity::class.java)
            intent.putExtras(it)
            startActivityForResult(intent, 0)
        }

        viewModel.defUI.callObserve.observe(this){
            viewModel.eqpItems.add(viewModel.entity)
            etEqpWork.setText("")
            tvFab.setText("")
            tvLine.setText("")
            tvPos.setText("")
            viewModel.entity = null
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (data != null) {
                if (requestCode == 1000) {
                    val stringExtra = data.getStringExtra("BITMAP")
                    viewModel.submit(this, stringExtra)
                } else {
                    val eqpInfoEntity = data.getSerializableExtra("DATA") as EqpInfoEntity?
                    if(eqpInfoEntity!!.NO == null || eqpInfoEntity.NO == ""){
                        eqpInfoEntity.NO = "0"
                    }
                    viewModel.eqpItems[(eqpInfoEntity.NO!!.toInt() - 1)] = eqpInfoEntity
                }

            }

        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvAddEqp -> {
                if (etEqpWork.text.toString().isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.eqp_hint), false)
                    return
                }

                if (viewModel.entity == null) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.eqp_hint), false)
                    return
                }

                if (tvFab.text.toString().isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.selector_fab), false)
                    return
                }

//                if (tvLine.text.toString().isEmpty()) {
//                    viewModel.defUI.toastEvent.value =
//                        ToastMessageBean(getString(R.string.selector_line), false)
//                    return
//                }

//                if (tvPos.text.toString().isEmpty()) {
//                    viewModel.defUI.toastEvent.value =
//                        ToastMessageBean(getString(R.string.selector_pos), false)
//                    return
//                }

                viewModel.addEqpToList(
                    tvFab.text.toString(),
                    tvLine.text.toString(),
                    tvPos.text.toString()
                )
            }
            R.id.flFabList -> {
                if (viewModel.fabList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }
                fabPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flFabList.width,
                    viewModel.fabList,
                    ivFabArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null"){
                                tvFab.text = ""
                            }else{
                                tvFab.text = codeS
                            }
                            tvLine.text = ""
                            tvPos.text = ""
                            if (codeS != "") {
                                viewModel.getLineList(codeS.split(":")[1])
                            }
                        }

                    })


                if (fabPop!!.isShowing) {
                    fabPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivFabArrow)
                    fabPop?.showAsDropDown(flFabList, 0, 0)
                }
            }
            R.id.flLineList -> {
                if (viewModel.lineList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }

                linePop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flLineList.width,
                    viewModel.lineList,
                    ivLineArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null"){
                                tvLine.text = ""
                            }else{
                                tvLine.text = codeS
                            }
                            tvPos.text = ""
                            if (codeS != ""){
                                viewModel.getPositionList(codeS.split(":")[1])
                            }
                        }

                    })



                if (linePop!!.isShowing) {
                    linePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivLineArrow)
                    linePop?.showAsDropDown(flLineList, 0, 0)
                }
            }
            R.id.flPosList -> {
                if (viewModel.posList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }

                posPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flPosList.width,
                    viewModel.posList,
                    ivPos,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null"){
                                tvPos.text = ""
                            }else{
                                tvPos.text = codeS
                            }
                        }

                    })



                if (posPop!!.isShowing) {
                    posPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivPos)
                    posPop?.showAsDropDown(flPosList, 0, 0)
                }
            }
        }

    }
}